package com.project.utils;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.layout.font.FontProvider;
import com.project.bo.TravelPdfBO;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.FileOutputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

public class GeneratePdf {

    public static String getTemplateContent(String templateDirectory, String templateName, Map<String, Object> paramMap) throws Exception {
        Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        try {
            configuration.setDirectoryForTemplateLoading(new File(templateDirectory));
        } catch (Exception e) {
            System.out.println("-- exception --");
        }

        Writer out = new StringWriter();
        Template template = configuration.getTemplate(templateName,"UTF-8");
        template.process(paramMap, out);
        out.flush();
        out.close();
        return out.toString();
    }

    public static boolean html2Pdf(String content, String outPath) {
        try {
            ConverterProperties converterProperties = new ConverterProperties();
            converterProperties.setCharset("UTF-8");
            FontProvider fontProvider = new FontProvider();
            fontProvider.addSystemFonts();
            converterProperties.setFontProvider(fontProvider);
            HtmlConverter.convertToPdf(content, new FileOutputStream(outPath), converterProperties);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public static String GenerateTravelPdf(TravelPdfBO travelPdfBO){
        Map<String, Object> paramMap = new HashMap<>();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        paramMap.put("theone",travelPdfBO.getPolicyholderName());
        paramMap.put("phone",travelPdfBO.getPolicyholderPhonenumber());
        paramMap.put("classic",travelPdfBO.getPolicyholderDocumenttype());
        paramMap.put("classnumber",travelPdfBO.getPolicyholderIdcardnumber());
        paramMap.put("theohterone",travelPdfBO.getInsuredName());
        paramMap.put("otherphone",travelPdfBO.getInsuredPhonenumber());
        paramMap.put("otherclassic",travelPdfBO.getInsuredDocumenttype());
        paramMap.put("otherclassnumber",travelPdfBO.getInsuredIdcardnumber());
//        paramMap.put("starttime", travelPdfBO.getInsurancestarttime());
//        paramMap.put("endtime",travelPdfBO.getInsuranceendtime());
        paramMap.put("starttime", dateTimeFormatter.format(LocalDateTime.now()));
        paramMap.put("endtime", dateTimeFormatter.format(LocalDateTime.now()).substring(0, 10));
        for (int i = 0; i < travelPdfBO.getResponsibilityList().size(); i++) {
            paramMap.put("name"+(i+1),travelPdfBO.getResponsibilityList().get(i).getDutyDescribe());
            paramMap.put("describ"+(i+1),travelPdfBO.getResponsibilityList().get(i).getDutyRange());
            paramMap.put("price"+(i+1),travelPdfBO.getResponsibilityList().get(i).getPrice());
        }
        travelPdfBO.setInsuranceamount(100);
        paramMap.put("money",travelPdfBO.getInsuranceamount());
        String outPath = "D:\\"+travelPdfBO.getPolicyholderName()+"家财险电子保单"+".pdf";
        ClassLoader classLoader = GeneratePdf.class.getClassLoader();
        URL resource = classLoader.getResource("template");
        String templateDirectory  = null;
        try {
            templateDirectory = resource.toURI().getPath();
            String templateContent = GeneratePdf.getTemplateContent(templateDirectory, "template.html", paramMap);
            GeneratePdf.html2Pdf(templateContent, outPath);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return outPath;
    }

}
